from time import sleep
class Clock:
    
    def __init__(self, quantum):
        self.cpu = None
        self.quantum = quantum
        self.sleeping = False
        
    def setQuantum(self,quantum):
        self.quantum = quantum
        self.sleeping = False
        self.tick()
        
    def setCpu(self, cpu):
        self.cpu = cpu
        
    def tick(self):
        while(not self.sleeping):
            self.cpu.fetch(self)
            if(self.quantum != None):
                if(self.quantum > 0):
                    self.quantum = self.quantum - 1
                else:
                    self.cpu.timeOut(self)
                    self.clockSleep()      
            sleep(0.3)
            
    def clockSleep(self):
        self.sleeping = True
        